/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//Shoots bullets randomly at one of the players and of a random color
//Push the bullets back at him to win.

package chromafinal;

import java.util.ArrayList;
import java.awt.*;
import java.util.Random;
/**
 *
 * @author Benjamin
 */
public class Boss extends DynamicBall{

    final int SHOT_SIZE = 20;


    ArrayList<Bullet> bullets;
    long stime;
    Player p[];
    int health;
    boolean alive;
    Random generator;

    public Boss(double x, double y, int m, int size0, int num0,
            Player p10, Player p20, Color bcolor)
    {
        super(x,y,m,size0,num0,bcolor);
        generator = new Random();
        p = new Player[2];
        p[0] = p10;
        p[1] = p20;
        stime = System.currentTimeMillis();
        yv = -25;
        alive = true;
        bullets = new ArrayList();
        health = 3;
    }

    public Bullet shoot(int target)
    {
        double direction[] = {p[generator.nextInt(2)].xl - xl, p[generator.nextInt(2)].yl - yl};
        double fmult = Math.pow(Math.pow(direction[0],2) +
                Math.pow(direction[1],2),0.5);
        double lmult = (size/2.0 + SHOT_SIZE/2.0)/fmult;


        Bullet bullet;

        int mod;
        Color color0;

        if (generator.nextInt(2) == 1)
        {
            mod = 1;
            color0 = Color.red;
        }
        else
        {
            mod = -1;
            color0 = Color.green;

        }


        bullet = new Bullet(lmult * direction[0] + xl, lmult * direction[1] + yl,600 * mod,
                SHOT_SIZE, 20,this, color0);
        bullet.xv = direction[0] * (250.0/fmult);
        bullet.yv = direction[1] * (250.0/fmult);
        return bullet;
    }

    public void move(int period)
    {
        super.move(period);

        Bullet tmp;

        for (int i = 0; i < bullets.size(); i++)
        {
            tmp = bullets.get(i);
            if (tmp.alive)
                tmp.move(period);
        }

        if(System.currentTimeMillis()-stime > 2000)
        {
            stime = System.currentTimeMillis();
            bullets.add(shoot(1));
        }

        if (yl > 400 || yl < 100)
            yv *= -1;
    }

    public void damage()
    {
        health--;
        if (health <= 0)
            alive = false;
    }

    public void draw(Graphics g)
    {
        super.draw(g);

        Bullet tmp;
        for(int i = 0; i < bullets.size(); i++)
        {
            tmp = bullets.get(i);
            if (tmp.alive)
                tmp.draw(g);
        }

        Font font = new Font("Arial", Font.BOLD, 20);
        g.setFont(font);
        g.setColor(Color.white);
        g.drawString( "HP: " + health, (int) xl - 20, (int)(yl - 70));

    }

    public void push()
    {
        Bullet tmp;
        for (int i = 0; i < bullets.size(); i++)
        {
            tmp = bullets.get(i);
            if (tmp.alive)
            {
                tmp.pushPlayer(p[0]);
                tmp.pushPlayer(p[1]);
                p[0].push(tmp);
                p[1].push(tmp);
            }
        }
    }

}
